/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

asc
{
  cbuffer MrtVSPara : register(c0) : slot vs 0
  {
    row_major float4x4 mvp;     // rotation for light vector (not really needed)
    float4x3 mv;
    float4x3 m;

    float4 ld[3];               // light direction
    float4 la;                  // ambient
    float4 lc[4];               // light color


    extern void Set(const sViewport &view,const sMaterialEnv &env)
    {
      mvp = view.ModelScreen;
      mv = view.ModelView;
      m = view.Model;
    }
  };

  cbuffer MrtPSPara : register(c0) : slot ps 0
  {
    float4 ld[3];               // light direction
    float4 la;                  // ambient
    float4 lc[4];               // light color

    extern void Set(const sViewport &view,const sMaterialEnv &env)
    {
      sVector30 l[4];

      l[0] = env.LightDir[0];
      l[1] = env.LightDir[1];
      l[2] = env.LightDir[2];
      l[3] = env.LightDir[3];

      ld[0].Init(-l[0].x,-l[1].x,-l[2].x,-l[3].x);
      ld[1].Init(-l[0].y,-l[1].y,-l[2].y,-l[3].y);
      ld[2].Init(-l[0].z,-l[1].z,-l[2].z,-l[3].z);
      la   .InitColor(env.AmbientColor);
      lc[0].InitColor(env.LightColor[0]);
      lc[1].InitColor(env.LightColor[1]);
      lc[2].InitColor(env.LightColor[2]);
      lc[3].InitColor(env.LightColor[3]);
    }
  };
}

/****************************************************************************/

material MrtRender
{
  vs
  {
    asc vs_2_0                 // hlsl code
    {
      use MrtVSPara;
      void main
      (
        in float3 in_pos : POSITION,
        in float3 in_norm : NORMAL,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float3 out_norm : TEXCOORD1,
        out float3 out_wpos : TEXCOORD2,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_norm = mul(float4(in_norm,0),m);
        out_wpos = mul(float4(in_pos,1),m);
        out_pos = mul(float4(in_pos,1),mvp);
      }
    }
  }

  ps
  {
    asc ps_2_0
    {
      sampler2D s0 : register(s0);
      void main
      (
        in float2 in_uv : TEXCOORD0,
        in float3 in_norm : TEXCOORD1,
        in float3 in_wpos : TEXCOORD2,
        out float4 out_col : COLOR0,
        out float4 out_norm : COLOR1,
        out float4 out_wpos : COLOR2,
      )
      {
        out_col = tex2D(s0,in_uv);
        out_wpos = float4(in_wpos,1);
        out_norm = float4(normalize(in_norm),0);
      }
    }
    
  }
};

material MrtBlit
{
  vs
  {
    asc vs_2_0                 // hlsl code
    {
      use MrtVSPara;
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }
    }
  }

  ps
  {
    asc ps_2_0
    {
      use MrtPSPara;
      sampler2D t_diff : register(s0);
      sampler2D t_norm : register(s1);
      void main
      (
        in float2 in_uv : TEXCOORD0,
        out float4 result : COLOR0,
      )
      {
        float4 diff = tex2D(t_diff,in_uv);
        float3 norm = normalize(tex2D(t_norm,in_uv).xyz);

        float4 i = ld[0]*norm.x + ld[1]*norm.y + ld[2]*norm.z;
        i = max(i,0);
        float4 l = la + lc[0]*i.x + lc[1]*i.y + lc[2]*i.z + lc[3]*i.w;

        result = diff*l;
      }
    }
  }
};
